#include <tmf_constants.h>
#include <tmf_macros.h>

#ifndef execNow
#define execNow call compile preprocessfilelinenumbers
#endif

INIT_DEBUG(0)

GVAR(INIT) = false;GVAR(SERVER) = isServer; GVAR(CLIENT) = !isDedicated; GVAR(HCCLIENT) = !isDedicated && !hasInterface; GVAR(JIP) = false;GVAR(SPE) = false;GVAR(MP) = isMultiplayer;
#define __waitpl 0 spawn {scriptName "spawn_WaitForNotIsNullPlayer";waitUntil {!isNull player}; GVAR(HCCLIENT) = !isDedicated && !hasInterface; GVAR(INIT) = true}
if (isServer) then {
	if (!isDedicated) then {
		GVAR(SPE) = true;
	};
	GVAR(INIT) = true;
} else {
	if (isNull player) then {
		GVAR(JIP) = true;
		__waitpl;
	} else {
		GVAR(HCCLIENT) = !isDedicated && !hasInterface;
		GVAR(INIT) = true;
	};
};
diag_log [QGVAR(INIT),GVAR(INIT),QGVAR(SERVER),GVAR(SERVER),QGVAR(CLIENT),GVAR(CLIENT),QGVAR(HCCLIENT),GVAR(HCCLIENT),QGVAR(JIP),GVAR(JIP),QGVAR(SPE),GVAR(SPE)];
sleep (random 0.005);

if (isnil "TMF_INITIALISING_BUSY") then { TMF_INITIALISING_BUSY = false; };
if (TMF_INITIALISING_BUSY) exitWith {};
TMF_INITIALISING_BUSY = true;

0 spawn {
	sleep 20;
	if (isNil "TMF_INITIALIZED" || {!TMF_INITIALIZED}) then {
		TMF_INITIALIZED = true;
		diag_log format["MSO-%1 CRITICAL ERROR: MSO/TMF Failed to initialize in time!!", time];
	};
};

enableSentences false;
0 fadeRadio 0;

idxHC = -1;
if(isServer) then {
	if (isNil "headlessClients") then {
        headlessClients = [];
        publicVariable "headlessClients";
	};
} else {
	0 spawn mso_core_fnc_initHC;
};

execNow "tmf\init_common.sqf";
if (isNil "TMF_BUILDING_POSITIONS_LIST") then { TMF_BUILDING_POSITIONS_LIST = []; };
execNow "tmf\common\building_position.sqf";
if (count TMF_BUILDING_POSITIONS_LIST > 0) then {
	{ execNow format["tmf\common\building_position_%1.sqf",_x];
	}foreach TMF_BUILDING_POSITIONS_LIST;
};

waitUntil{!isNil "BIS_fnc_init"};

CBA_ui_fnc_add = {};
cba_fnc_replace = {};

if (!isDedicated) then {
	if (isnil "server" || isnil QUOTE(__JIPH__)) exitWith {
		0 spawn {
			while { true } do {
				hintSilent parseText(localize "STR_TMF_MISSING_SERVER");
				sleep 0.5;
			};
		};
	};
	if (!isMultiplayer && (!(IS_MARKER("respawn_west")) && !(IS_MARKER("respawn_east")) && !(IS_MARKER("respawn_guerilla")) && !(IS_MARKER("respawn_civilian")))) exitWith {
		0 spawn {
			for "_i" from 0 to 20 do {
				hintSilent parseText(localize "STR_TMF_MISSING_RESPAWN");
				sleep 0.5;
			};
		};
	};
};

if(isNil "debug_mso_setting") then {debug_mso_setting = 0;};
if(debug_mso_setting == 0) then {debug_mso = false; debug_mso_loc = false;};
if(debug_mso_setting == 1) then {debug_mso = true; debug_mso_loc = false;};
if(debug_mso_setting == 2) then {debug_mso = true; debug_mso_loc = true;};
publicvariable "debug_mso";
publicvariable "debug_mso_loc";

if (!isNil "paramsArray") then {
	if (debug_mso) then { diag_log format["MSO-%1 Mission Parameters", time]; };
	for "_i" from 0 to ((count paramsArray)-1) do {
			_name = configName ((missionConfigFile/"Params") select _i);
			if (isnil _name) then {
				missionNamespace setVariable [_name,paramsArray select _i];
				if (debug_mso) then { diag_log format["MSO-%1    %2 = %3", time, configName ((missionConfigFile/"Params") select _i), paramsArray select _i]; };
			};
	};
};

if (mso_core_show_initialisations <= 0) then {
	titleCut ["", "BLACK FADED", 999];
	titleText ["Initialising...", "BLACK"];
};

execNow "tmf\core\init.sqf";
execNow "tmf\mission\init.sqf";
execNow "tmf\support\init.sqf";

"Completed" call mso_core_fnc_initStat;
diag_log format["MSO-%1 Initialisation Completed", time];
if (mso_core_show_initialisations <= 0) then {
	titleCut ["", "BLACK IN", 3];
	titleText ["Initialisation Completed", "BLACK IN"];
};

if (isServer) then {
	execNow "tmf\init_server.sqf";
};
if (!isDedicated) then {
	execNow "tmf\init_client.sqf";
};

TMF_INITIALIZED = true;
TMF_INITIALISING_BUSY = false;